package lesson11;

import lesson05.Test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestFrame01 extends JFrame implements ActionListener {
    private JButton btn1 = new JButton("按钮1");
    private JButton btn2 = new JButton("按钮2");
    private JTextField textField = new JTextField();
    private int index;
    public TestFrame01(){
        this.setLayout(null);
        btn1.setBounds(0,0,100,30);

        //建立监听器和事件源的联系
        btn1.addActionListener(this);

        btn2.setBounds(110,0,100,30);
        btn2.addActionListener(this);

        textField.setBounds(220,0,250,30);

        this.add(btn1);
        this.add(btn2);
        this.add(textField);
        this.setSize(800,600);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new TestFrame01();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("点击按钮:" + e.getActionCommand());
        System.out.println(e.getSource());
//        if(e.getActionCommand().equals("按钮1")){
//            System.out.println("点击按钮1");
//        }else if(e.getActionCommand().equals("按钮2")){
//            System.out.println("点击按钮2");
//        }

        if(e.getSource() == btn1){
            index++;
            textField.setText(index + "");
        }else if(e.getSource() == btn2){
            System.out.println("点击按钮2");
        }
    }
}
